package com.http;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.AsyncTask;

import com.android.ocean.util.net.ErrorCode;
import com.android.ocean.util.net.NetConUtil;
import com.android.ocean.util.net.ResEntity;
import com.common.method.CommonData;
import com.common.method.ServerPath;
import com.modle.User;
import com.util.JsonUtil;

/**
 * 
 * @ClassName:UserInfoTask
 * @Description:TODO获取用户信息
 * @author zhuw
 * @date 2013-8-3 下午12:05:47
 */
public class UserInfoTask extends AsyncTask<String, Void, ResultUser> {
	public static interface CallBack extends NetWorkCallBack {
		
		public abstract void procUserInfoData(User data);
		public abstract void procUserInfoError(String msg);

	}

	CallBack callback;

	public UserInfoTask(CallBack callback) {
		super();
		this.callback = callback;
	}

	@Override
	protected ResultUser doInBackground(String... params) {
		ResultUser data = null;
		try {
			ResEntity re=NetConUtil.sendGetRequestEntity(ServerPath.USERINFO_PATH+params[0]);
			if(re.code==ResEntity.S_OK){
				User user=JsonUtil.parseUser(new JSONObject(re.jsonStr));
				
			    data= new ResultUser(CommonData.H_FLAG_SUCCESS, null, user);
			}else if(re.code==ResEntity.S_FAILED){
				
				  data= new ResultUser(CommonData.H_FLAG_ERROR, "code:"+re.code+"    "+re.jsonStr, null);
				
			}else{
				  data= new ResultUser(CommonData.H_FLAG_ERROR, "code:"+re.code+ErrorCode.ERROR_MSG, null);
			}
		} catch (Exception e) {
			data= new ResultUser(CommonData.H_FLAG_ERROR, ErrorCode.ERROR_MSG, null);
			e.printStackTrace();

		}
		return data;
	}

	@Override
	protected void onPostExecute(ResultUser result) {
		if (result.flag == CommonData.H_FLAG_SUCCESS) {
			callback.procUserInfoData(result.data);

		} else {
			callback.procUserInfoError(result.msg);
		}

	}
}
